﻿using MCMesServer.DBFunctions.TransformFuncs;
using MesBLL.EventService;
using MesDAL.DBInfo;
using MesDAL.DTO;
using MesDAL.DTO.ProcessSheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.DBFuncs
{
    public class ProduceplanDBFunc
    {
        public async static Task<ProduceplanDTO> SelectProduceplanByProcessSheet(ProcessSheetDTO dto)
        {
            using (var ser = new ProducePlanService())
            {
                produceplan_log log = await ser.SelectProduceplanByProcessSheet(dto.id);
                if (log is null)
                {
                    return null;
                }
                else
                {
                    ProduceplanDTO producePlan = TransformDTOFunc.TransformModel2ProduceplanDTO(log);
                    return producePlan;
                }
            }
        }

        public async static Task<List<ProduceplanDTO>> SelectProduceplanWithMachineAndDate(MachineInfoDTO machine, DateTime date)
        {
            using (var ser = new ProducePlanService())
            {
                List<produceplan_log> log_list = await ser.SelectProduceplanWithMachineAndDate(machine.machine_num, date);
                List<ProduceplanDTO> list = new List<ProduceplanDTO>();
                for (int i = 0; i < log_list.Count; i++)
                {
                    list.Add(TransformDTOFunc.TransformModel2ProduceplanDTO(log_list[i]));
                }
                return list;
            }
        }

        public async static Task<ProduceplanDTO> AddNewProduceplanDTO(ProduceplanDTO planDTO)
        {
            using (var ser = new ProducePlanService())
            {
                produceplan_log log = TransformDTOFunc.TransfromProducePlan2Model(planDTO);
                produceplan_log addLog = await ser.AddAsync(log);
                planDTO.id = addLog.id;
                return planDTO;
            }
        }

        /// <summary>
        /// 检索指定日期内未完成的计划单
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public async static Task<List<ProduceplanDTO>> SelectUnFinishProducePlanByDate(DateTime date)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> plan_list = await ser.SelectUnFinishProducePlanByDate(date);
                return plan_list;
            }
        }

        /// <summary>
        /// 提高计划单优先级
        /// </summary>
        /// <param name="planId"></param>
        /// <returns></returns>
        public async static Task<List<ProduceplanDTO>> EditIncreasePriority(List<ProduceplanDTO> list)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> rebackList = new List<ProduceplanDTO>();
                for (int i = 0; i < list.Count; i++)
                {
                    produceplan_log log = TransformDTOFunc.TransfromProducePlan2Model(list[i]);
                    log = await ser.EditAsync(log);
                    if (log.id == list[i].id)
                    {
                        rebackList.Add(list[i]);
                    }
                }
                return rebackList;
            }
        }

        /// <summary>
        /// 检索当前设备以及日期下，最近一件计划表
        /// </summary>
        /// <param name="machineInfo"></param>
        /// <param name="estimated_start"></param>
        /// <returns></returns>
        internal async static Task<ProduceplanDTO> SelectLatestPlanWithMachineAndDate(MachineInfoDTO machineInfo, DateTime estimated_start)
        {
            using (var ser = new ProducePlanService())
            {
                ProduceplanDTO latestLog = await ser.SelectLatestPlanWithMachine(machineInfo.machine_num);
                return latestLog;
            }
        }

        /// <summary>
        /// 删除已有的生产计划
        /// </summary>
        /// <param name="planDTO"></param>
        internal async static Task<bool> DeleteProducePlan(ProduceplanDTO planDTO)
        {
            using (var ser = new ProducePlanService())
            {
                bool isDelete = await ser.DeleteAsync(planDTO.id);
                return isDelete;
            }
        }

        /// <summary>
        /// 检索当前日期往后的所有计划单
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        internal async static Task<List<ProduceplanDTO>> SelectProduceplanAfterDate(DateTime date)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> log_list = await ser.SelectUnFinishProducePlanAfterDate(date);
                return log_list;
            }
        }

        /// <summary>
        /// 检索设备中未完成的计划单
        /// </summary>
        /// <param name="machineNum"></param>
        /// <returns></returns>
        internal async static Task<List<ProduceplanDTO>> SelectUnFinishProducePlanWithMachine(string machineNum)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> selectList = await ser.SelectUnFinishProducePlanByMachineNum(machineNum);
                return selectList;
            }
        }

        internal async static Task<List<ProduceplanDTO>> SelectUnFinishProducePlanWithMachineId(int machineId)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> selectList = await ser.SelectUnFinishProducePlanByMachineId(machineId);
                return selectList;
            }
        }

        internal async static Task<List<ProduceplanDTO>> EditProducePlanList(List<ProduceplanDTO> needEditList)
        {
            using (var ser = new ProducePlanService())
            {
                List<ProcessSheetDTO> processList = new List<ProcessSheetDTO>();
                foreach (var item in needEditList)
                {
                    processList.Add(item.processSheet);
                }
                ProcessSheetDBFunc.EditProcessSheetList(processList);
                bool isEditSuccess = await ser.EditProducePlanList(needEditList);
                return needEditList;
            }
        }

        /// <summary>
        /// 检索当前所有未完成的任务单
        /// </summary>
        /// <returns></returns>
        internal async static Task<List<ProduceplanDTO>> SelectAllUnFinishProducePLan()
        {
            using (var ser = new ProducePlanService())
            {
                List<ProduceplanDTO> list = await ser.SelectAllUnFinishProducePlan();
                return list;
            }
        }


    }
}
